package com.usian.controller;

import com.usian.feign.ContentFeign;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.utils.PageResult;
import com.usian.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/content")
public class ContentControllerWeb {

    @Autowired
    private ContentFeign contentFeign;

    /**
     * 内容分类管理查询
     * @param id
     * @return
     */
    @RequestMapping("selectContentCategoryByParentId")
    public Result selectContentCategoryByParentId(@RequestParam(defaultValue = "0") Long id){
        List<TbContentCategory> list = contentFeign.selectContentCategoryByParentId(id);
        if (list.size()>0){
            return Result.ok(list);
        }
        return Result.error("查无结果");
    }

    /**
     * 内容分类管理添加
     * @param parentId
     * @param name
     * @return
     */
    @RequestMapping("insertContentCategory")
    public Result insertContentCategory(@RequestParam Long parentId,@RequestParam String name ){
        Integer num = contentFeign.insertContentCategory(parentId,name);
        if (num>0){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 内容分类管理删除
     * @param categoryId
     * @return
     */
    @RequestMapping("deleteContentCategoryById")
    public Result deleteContentCategoryById(@RequestParam Long categoryId){
        Integer num = contentFeign.deleteContentCategoryById(categoryId);
        if (num>0){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }

    /**
     * 内容分类管理修改
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("updateContentCategory")
    public Result updateContentCategory(@RequestParam Long id,@RequestParam String name){
        Integer num = contentFeign.updateContentCategory(id, name);
        if (num>0){
            return Result.ok("修改成功");
        }
        return Result.error("修改失败");
    }

    /**
     * 内容管理	查询
     * @param categoryId
     * @return
     */
    @RequestMapping("selectTbContentAllByCategoryId")
    public Result selectTbContentAllByCategoryId(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "5")Integer rows,@RequestParam Long categoryId){
        PageResult pageResult = contentFeign.selectTbContentAllByCategoryId(page,rows,categoryId);
        if (pageResult.getResult().size()>0){
            return Result.ok(pageResult);
        }
        return Result.error("查无结果");
    }

    /**
     * 内容管理  添加
     * @param tbContent
     * @return
     */
    @RequestMapping("insertTbContent")
    public Result insertTbContent(TbContent tbContent){
        Integer num = contentFeign.insertTbContent(tbContent);
        if (num>0){
            return Result.ok("添加成功");
        }
        return Result.error("添加失败");
    }

    /**
     * 内容管理  删除
     * @param ids
     * @return
     */
    @RequestMapping("deleteContentByIds")
    public Result deleteContentByIds(@RequestParam Long ids){
        Integer num = contentFeign.deleteContentByIds(ids);
        if (num>0){
            return Result.ok("删除成功");
        }
        return Result.error("删除失败");
    }
}
